<?php
require("bases.php");

class catocourse extends bases {

	public function __construct(){
		parent::__construct();
		try{
			$this->fmenu = $this->controller = "catocourse";
			$this->position_one = array("text"=>"实验管理");
			$obj_catocourse = spClass('cato_course');
			$level = intval($this->spArgs()['level']);
			$this->level = $level;
			$this->level_name =  $level == 1? '目录': ($level == 2 ? '课程': '实验');
			$this->cato = $obj_catocourse->findAll(array('isdel'=>0, 'level_id'=>1), "sort asc, id desc", 'id,name');
			$this->course = $obj_catocourse->findAll(array('isdel'=>0, 'level_id'=>2,'parent_id'=>1), "sort asc, id desc", 'id,name');
		}catch(Exception $e){

		}
	}

	public function datalist(){
		$obj_catocourse = spClass('cato_course');
		$postdata = $this->spArgs();
		$page = intval(max($postdata['page'], 1));
		$condition = "1 and isdel = 0 ";
		if($this->level){
			$condition .= " and level_id = ".$this->level;
		}
		if(!$this->level){
			$condition .= " and level_id = 1";
			$data_array = $obj_catocourse->findAll($condition,'sort asc, id desc');
			$data_rs = array();
			foreach($data_array as $k => &$v){
				$course_rs = $obj_catocourse->findAll(array('status'=>1,'isdel'=>0,'level_id'=>2,'parent_id'=>$v['id']), "sort asc, id desc");
				$data_rs[] = $v;
				if($course_rs){
					foreach($course_rs as $k1 =>&$v1){
						$test_rs = $obj_catocourse->findAll(array('status'=>1,'isdel'=>0,'level_id'=>3,'parent_id1'=>$v['id'],'parent_id'=>$v1['id']), "sort asc, id desc");
						$data_rs[] =$v1;
						foreach($test_rs as $k2 => &$v2){
							$data_rs[] =$v2;
						}	
					}
				}
			}
		}else{
			$data_rs = $obj_catocourse
				->spPager($page, 10)->findAll($condition,  "sort asc, id desc");
		}
		$this->data_rs =$data_rs;
		$this->pager = $obj_catocourse->spPager()->getPager();
		$this->url = spUrl('catocourse', 'datalist', array("searchkeys"=>$this->searchkeys));
		$this->position_two = array("text"=>$this->level_name."列表", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>$this->level_name."列表");
		$this->searchform = 1;
	}

	public function create(){
		$postdate = $this->spArgs();
		$obj_catocourse = spClass('cato_course');
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			try{
				$data = array();
				//客户端提交方法
				$data['name'] = $postdate['name'];
				$data['level_id'] =$postdate['level'];
				if($postdate['parent_cato']){
					$data['parent_id'] = $data['parent_id1'] = intval($postdate['parent_cato']);
				}
				if($postdate['parent_course']){
					$data['parent_id'] = intval($postdate['parent_course']);
					$data['parent_id1'] = $obj_catocourse ->find(array('id'=>intval($postdate['parent_course'])),null,'id,name,parent_id')['parent_id'];
				}
				$data["sort"] = intval($postdate["sort"]);
				if(!$obj_catocourse->create($data))
					throw new Exception("未知错误，".$this->level_name."添加失败");
				$url = spUrl($this->controller, "datalist");
				$message = array('msg'=>$this->level_name.'添加成功！','result'=>1, 'url'=>$url);
				echo json_encode($message);
				exit();
			}catch(Exception $e){
				$message = array('msg'=>$e->getMessage() ,'result'=>-1);
				echo json_encode($message);
				exit();
			}
		}
		$info_rs = array(
			"parent_id1" => intval($postdate["level1"]),
			"parent_id" => intval($postdate["level2"])
		);
		if($info_rs["parent_id1"]){
			$this->course_level1_rs = $obj_catocourse->find(array('isdel'=>0, 'level_id'=>1, 'id'=>$info_rs["parent_id1"]), "sort asc, id desc", 'id,name');
		}
		if($info_rs["parent_id"]){
			$this->course_level2_rs = $obj_catocourse->find(array('isdel'=>0, 'level_id'=>2, 'id'=>$info_rs["parent_id"]), "sort asc, id desc", 'id,name');
		}
		$this->info_rs = $info_rs;
		$this->saveurl = spUrl("catocourse", "create");
		$this->position_two = array("text"=>$this->level_name."添加", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>$this->level_name."添加");
	}

	function modify(){
		try {
			$postdata = $this->spArgs();
			$obj_catocourse = spClass('cato_course');
			if(!$id = intval($postdata['id']))
				throw new Exception("非法操作");
			if(!$info_rs = $obj_catocourse->find(array("id"=>$id)))
				throw new Exception("找不到该信息，可能已被删除");
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				try{
					$data = array();
					//客户端提交方法
					$data['name'] = $postdata['name'];
					if($postdata['parent_cato']){
						$data['parent_id'] = $data['parent_id1'] = intval($postdata['parent_cato']);
					}
					if($postdata['parent_course']){
						$data['parent_id'] = intval($postdata['parent_course']);
						$data['parent_id1'] = $obj_catocourse ->find(array('id'=>intval($postdata['parent_course'])),null,'id,name,parent_id')['parent_id'];
					}
					$data["sort"] = intval($postdata["sort"]);
					if(!$obj_catocourse->update(array("id"=>$id), $data))
						throw new Exception("未知错误，".$this->level_name."更新失败");
					$url = spUrl($this->controller, "datalist");
					$message = array('msg'=>'修改成功！','result'=>0, 'url'=>$url);
					echo json_encode($message);
					exit();
				}catch(Exception $e){
					$message = array('msg'=>$e->getMessage() ,'result'=>-1);
					echo json_encode($message);
					exit();
				}
			}
		}catch(Exception $e){
			$this->redirect(spUrl($this->controller, "datalist"), "非法操作！");
		}
		$this->id = $id;
		$this->info_rs = $info_rs;
		if($info_rs["level_id"] > 1){
			if($info_rs["parent_id1"]){
				$this->course_level1_rs = $obj_catocourse->find(array('isdel'=>0, 'level_id'=>1, 'id'=>$info_rs["parent_id1"]), "sort asc, id desc", 'id,name');
			}
		}
		if($info_rs["level_id"] > 2){
			if($info_rs["parent_id"]){
				$this->course_level2_rs = $obj_catocourse->find(array('isdel'=>0, 'level_id'=>2, 'id'=>$info_rs["parent_id"]), "sort asc, id desc", 'id,name');
			}
		}
		$this->saveurl = spUrl($this->controller, "modify");
		$this->position_two = array("text"=>$this->level_name."修改", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>$this->level_name."修改");
		$this->display($this->controller."/create.html");
	}

	public function delete(){
		$id = $this->spArgs('id');
		if($id){
			$obj_catocourse = spClass('cato_course');
			$url = spUrl($this->controller, "datalist");
			$conditions = array('id' => $id);
			$subcato = $obj_catocourse->getSubCato($id);
			if($subcato){
				if($obj_catocourse->update($conditions,array('isdel' =>1))){
					echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
					exit();
				}
			}else{
				echo json_encode(array('msg'=>'该目录下还有子目录，请先删除子目录后再删除！' , 'result'=>0, 'url'=>$url));
				exit();
			}

		}else{
			echo json_encode(array('msg'=>'请先选择删除项，再进行操作！' , 'result'=>0));
			exit();
		}
	}
	
	public function getClasses(){
		try{
			$postdata = $this->spArgs();
			if($postdata['cato_id']){
				try{
					$obj_catocourse = spClass("cato_course");
					$list = $obj_catocourse ->findAll(array('status'=>1,'isdel'=>0,'level_id'=>2,'parent_id'=>intval($postdata['cato_id'])));
					if($list){
						echo json_encode(array('data'=>$list , 'result'=> 1));
						exit;
					}else{
						throw new Exception("未知错误，该目录下没有课程，请添加课程");
					}
				}catch(Exception $e){
					$message = array('msg'=>$e->getMessage() ,'result'=>-1);
					echo json_encode($message);
					exit();
				}
			}
		}catch(Exception $e ){
			$this->redirect(spUrl($this->controller, "datalist"), "非法操作！");
		}
	}
	
}
?>